package com.magenta.maxoptra.routing;

import com.magenta.maxoptra.gis.GeoException;
import com.magenta.maxoptra.gis.RoutingCoordinate;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;

/*
* Project: MaxOptRA v1.2
* User: Ivan Usalko
* Date: 13.09.2009 22:52:44
*
* Copyright (c) 1999-2009 Magenta Corporation Ltd. All Rights Reserved.
* Magenta Technology proprietary and confidential.
* Use is subject to license terms.
*/
@MessageDriven(
        activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = SampleGeoInfoRequester.QUEUE_NAME)
        },
        mappedName = SampleGeoInfoRequester.JNDI_NAME,
        name = SampleGeoInfoRequester.JNDI_NAME,
        description = SampleGeoInfoRequester.DESCRIPTION
)
public class SampleGeoInfoRequester extends AbstractRequestWorkflow {

    public static final String JNDI_NAME = "GeoInfoRequestMDB";
    public static final String DOMAIN = "test";
    public static final String QUEUE_NAME = "queue" + '/' + DOMAIN + '-' + "route-requests";
    public static final String DESCRIPTION = "<b>Queue for geo info routes requests.</b>";

    @Override
    protected boolean checkMessage(Message message) {
        if (message instanceof BytesMessage) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean parseMessage(Message message) throws JMSException {
        BytesMessage checkedMessage = (BytesMessage) message;
        float latFrom = checkedMessage.readFloat();
        float latTo = checkedMessage.readFloat();
        float lonFrom = checkedMessage.readFloat();
        float lonTo = checkedMessage.readFloat();
        setFrom(new RoutingCoordinate("from", latFrom, lonFrom));
        setTo(new RoutingCoordinate("to", latTo, lonTo));
        return true;
    }

    @Override
    protected boolean stopWorkflowAfterException(GeoException e) {
        return false;
    }

//    private class Coords implements GeographicCoordinates {
//        private final double lat;
//        private final double lon;
//
//        public Coords(float lat, float lon) {
//            this.lat = lat;
//            this.lon = lon;
//        }
//
//        public double getLatitude() {
//            return lat;
//        }
//
//        public double getLongitude() {
//            return lon;
//        }
//    }
}
